package com.qy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qy.entity.Student;
import com.qy.mapper.StudentMapper;
import com.qy.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 袁大毛
 * * @date 2024/10/12 上午10:06
 * @描述
 */
@Controller
@ResponseBody
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    @GetMapping("/getById")
    public R getById(Integer id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        if (student == null) {
            return R.error("查询失败");
        } else {
            return R.ok(student);
        }
    }

    @PostMapping("/add")
    public R add(@RequestBody Student student) {
        System.out.println(student);
        int i = studentMapper.insertSelective(student);
        System.out.println(student);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error("添加失败");
        }
    }

    @PutMapping("/update")
    public R update(@RequestBody Student student) {
        int i = studentMapper.updateByPrimaryKeySelective(student);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error("修改失败");
        }
    }

    @DeleteMapping("/delete")
    public R delete(Integer id) {
        int i = studentMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error("删除失败");
        }
    }
    // 查询所有
    @GetMapping("/getAll")
    public R getAll(int pageNum, int pageSize){
        //开启分页       页码          一页几个
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = studentMapper.selectAll();
        PageInfo<Student> pageInfo=new PageInfo<>(students);//把查询的结果封装到PageInfo类型
        if (students == null){
            return R.error("查询失败");
        }else {
            return R.ok(200,"删除成功",pageInfo);
        }
    }
}
